const utils = require("../../utils/util.js");
// pages/group/group.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    groupId: "",
    wxGroupId: "",
    memberList: [],
    selfInfo: {},
    showResult: false,
    total: 0,
    groupAvatars: []
  },
  quitGroupTip() {
    const _this = this;
    wx.showModal({
      title: "提示",
      content: "确定要退出该群吗？",
      showCancel: true,
      success(res) {
        if (res.confirm) {
          _this.quitGroup();
        }
      }
    });
  },
  quitGroup() {
    utils
      .ajax("/v1/bcs/leaveUserCardGroup.do", {
        groupId: this.data.groupId
      })
      .then(res => {
        if (res.code === "000000") {
          utils.message("退群成功");
          wx.navigateBack({
            delta: 1
          });
        }else{
          console.log(res.message || "退出群失败");
        }
      })
      .catch(res => {});
  },
  getGeroupDetail(again) {
    utils
      .ajax("/v1/bcs/queryUserCardGroupDetails.do", {
        groupId: this.data.groupId
      })
      .then(res => {
        if (res.code === "000000") {
          // 取出群头像
          let groupAvatars = [res.data.selfCardInfo.avatarUrl];
          for (let i = 0; i < 3; i++) {
            if (res.data.datas[i]) {
              groupAvatars.push(res.data.datas[i]["avatarUrl"]);
            }
          }
          this.setData({
            memberList: res.data.datas,
            showResult: true,
            total: res.data.total,
            selfInfo: res.data.selfCardInfo,
            groupAvatars
          });
        }else{
          console.log(res.message || "查询名片群信息失败");
          wx.navigateBack({
            delta: 1
          });
        }
      })
      .catch(res => {});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("options", options);
    this.setData({
      groupId: options.id || "20180506000000000000000000000206",
      wxGroupId: options.wid || "GiJ495VewteF3QAKCeNseJsiFz-0"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    utils
      .preCheck()
      .then(res => {
        this.getGeroupDetail();
      })
      .catch(res => {});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === "button") {
      return {
        title: "快名片助手",
        path: "/pages/groups/groups",
        success(res) {
          console.log(res);
        },
        fail(res) {
          console.log(res);
        }
      };
    } else {
      return {
        title: "快名片助手",
        path: "/pages/index/index"
      };
    }
  }
});
